package com.houseman.workFlow.processer.impl;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.houseman.workFlow.bean.Line;
import com.houseman.workFlow.bean.TaskArea;
import com.houseman.workFlow.bean.WorkFlowGraph;
import com.houseman.workFlow.processer.Processer;
import com.houseman.workFlow.utils.BeanToXml;
import com.houseman.workFlow.utils.JsonToGraph;

/**
 * 保存流程图信息，以XML文件返回客户端
 */
public class SaveXmlProcesser extends Processer{

	@Override
	public void proccess(HttpServletRequest request, HttpServletResponse response) {
		
		String gJson = request.getParameter("graph");
		String tJson = request.getParameter("tasks");
		String lJson = request.getParameter("lines");
		
		JsonToGraph util = new JsonToGraph();
		
		WorkFlowGraph graph = util.toGraph(gJson);
		List<TaskArea> tasks = util.toTaskArea(tJson);
		List<Line> lines = util.toLine(lJson);
		
		graph.setTasks(tasks);
		graph.setLines(lines);
		
		BeanToXml btx = new BeanToXml();
		
		String xml = btx.xmlToBean(graph);
		
		response.reset();
		response.addHeader("Content-Disposition", "attachment; filename=\"" + graph.getName() + "\"");
		response.setContentType("application/octet-stream");
		response.setCharacterEncoding("UTF-8");
		
		try {
			PrintWriter out = response.getWriter();
			out.write(xml);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

}
